
angular.module('app').controller('addressAddCtrl',
    ['$scope', '$rootScope', '$http', 'utils', '$stateParams', 'activity','$state','userInfo',
        function ($scope, $rootScope, $http, utils, $stateParams, activity,$state,userInfo) {
            var reg =  /^1[34578][0-9]{9}$/;
            var reg2 =  /^1[34578][0-9-*]{9}$/;
            $scope.goback=function () {
                window.history.go(-1);
            };
            var picker = new mui.PopPicker({
                layer: 3
            });

            $scope.postPhone='';
            if($scope.isapp){
                appload={
                    CallBack:function (type) {
                        if(type=='5'){
                            $scope.saveMyAddress();
                        }
                    }
                };
            }
            $http.get('json/data.json').then(function (res) {
                picker.setData(res);
            });

            function filterPhone(mobile) {
                if(mobile) {
                    mobile = mobile.toString();
                    return   mobile.substr(0, 3) + "****" + mobile.substr(7, 11)
                } else {
                    return mobile
                }

            }
            $(".mui-mui-poppicker-btn-cancel").css('fount-size',"0.3rem");
            $(".mui-poppicker-btn-ok").css('fount-size',"0.3rem");
            $(".mui-pciker-list").css('fount-size',"0.3rem");
            $scope.$on('$viewContentLoaded', function(event) {
                if($stateParams.editedAddress){
                    $scope.editedAddress=$stateParams.editedAddress;
                    $scope.editName=$stateParams.editedAddress.consignee;
                    $scope.postPhone=$stateParams.editedAddress.phone;
                    $scope.editPhone= filterPhone($stateParams.editedAddress.phone);
                    $scope.editPca=$stateParams.editedAddress.addressPCA;
                    $scope.editAddress=$stateParams.editedAddress.address;
                    $scope.provinceId=$stateParams.editedAddress.provinceCode;
                    $scope.cityId=$stateParams.editedAddress.cityCode;
                    $scope.areaId=$stateParams.editedAddress.areaCode;
                    $scope.addressId=$stateParams.editedAddress.addressId;
                    $("#select-address-input").css('color','#333333');
                    if($rootScope.isapp) {
                        wv.setHeader('编辑收货地址','1','保存','5')
                    }
                }else {
                    $scope.editPca='请选择省、市、区';
                    if($rootScope.isapp) {
                        wv.setHeader('新增收货地址','1','保存','5')
                    }

                }
            });

            /**
             * 选择地址
             */
            $scope.selectAddress=function () {
                picker.show(function(items) {
                    var pr= (items[0] || {}).text
                    var ci=( items[1] || {}).text
                    var ar=( items[2] || {}).text
                    $scope.editPca= pr+" "+ci+ " "+ar;
                    $("#select-address-input").css('color','#333333');
                    //返回 false 可以阻止选择框的关闭
                    $scope.provinceId=$scope.showAddress((items[0] || {}).value);
                    $scope.cityId=$scope.showAddress((items[1] || {}).value);
                    $scope.areaId=$scope.showAddress((items[2] || {}).value);
                    $scope.$apply()
                });
            };


            /**
             * 回退监听
             */
            $scope.$on('$stateChangeStart', function(event) {
                if (picker) {
                    picker.hide()
                }
            });

            $scope.$watch('editPhone',function (newVal,oldVal) {
                if(!reg.test($scope.editPhone)){
                    if($stateParams.editedAddress){
                        if(!$scope.isPostPhon){
                            $scope.postPhone=$stateParams.editedAddress.phone;
                        }else {
                            for (var i = 0; i < $scope.editPhone.length; i++) {
                                var obj = $scope.editPhone[i];
                                if(!isNaN(obj)) {
                                    $scope.editPhone.replace(obj,"");
                                }
                            }
                            $scope.postPhone=$scope.editPhone;
                        }
                        $scope.isPostPhon=true;
                    }
                }else {
                    $scope.postPhone=$scope.editPhone;
                }

            });


            $scope.setPhone=function () {
                if(reg2.test($scope.editPhone)){
                    $scope.editPhone=''
                }
            };
            /**
             * 保存地址
             */
            $scope.saveMyAddress=function () {
              if($rootScope.isapp)wv.keyboardHide();
                    if(!$scope.editName){
                        utils.msg('请输入姓名');
                        return
                    }
                    if(!$scope.editPhone){
                        utils.msg('请输入手机号码');
                        return;
                    }
                    if(!reg.test($scope.postPhone)){
                        utils.msg('请输入正确手机号码');
                        return;
                    }

                    if(!$("#select-address-input").text()){
                        utils.msg('请选择所在地址');
                        return;
                    }
                    if(!$scope.editAddress){
                        utils.msg('请输入详细地址');
                        return;
                    }
                    $http.get(utils.integralSys, {
                        params: {
                            header:$stateParams.editedAddress?utils.header("300430"):utils.header("300410"),
                            body: {
                                address: $scope.editAddress, //收货详细地址
                                addressId:$scope.addressId,
                                areaId: $scope.areaId,  //收货区域
                                cityId: $scope.cityId, //城市
                                consignee: $scope.editName,  //收货人姓名
                                phone:$scope.postPhone, //电话
                                provinceId: $scope.provinceId,  //省
                                userCode:activity.getSoloCode(),
                                isDefault:$stateParams.editedAddress?$stateParams.editedAddress.isDefault+'':'2'
                            }
                        }
                    }).then(function(res) {
                        if(res.header.re_code == "0000") {
                            if(!$stateParams.editedAddress){
                                utils.msg('添加成功');
                            }
                            window.history.go(-1);

                        }
                    })

            };


            $scope.showAddress=function (res) {
                if(res){
                    return res;
                }else {
                    return '';
                }
            };

            $(document).click(function(e){
                var touchElement=$(e.target)
               var  elementClass=touchElement.attr("class")
                if(elementClass&&elementClass.indexOf('clear-x-input')>-1){
                    $('.clear-x').hide();
                    $(touchElement).siblings('.clear-x').show();
                }else {
                   $('.clear-x').hide();
                }

            });
            $('.clear-x').click(function () {
                $(this).siblings('input.clear-x-input').val("").trigger('input')
                $scope.$apply();
                $(this).show();
            })


            $('.clear-x-group').children('input.clear-x-input').on('focus',function (elemt) {
              var clearX=$(this).siblings('.clear-x')
               clearX.show();

            });


        }]);